package com.fh.shop.consumer;

import com.fh.shop.biz.IOrderCommonService;
import com.fh.shop.config.MQConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;


@Component
@Slf4j
public class OrderConsumer {
    @Resource(name = "orderCommonService")
    private IOrderCommonService orderCommonService;

    @RabbitListener(queues = MQConstant.orderDeadDirect.QUEUE)
    public void handlerOrder(String orderId, Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            orderCommonService.cancelOrder(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("取消订单异常,原因:",e.getMessage());
        } finally {
            channel.basicAck(deliveryTag,false);
        }
    }
}
